const express = require('express');
const jwt = require('jsonwebtoken');
const { secret } = require('../config/global');
// 导入文件上传中间件
const { upload } = require('../utils/upload');
const { login, verifyCode, checkToken, refreshToken } = require('./front/login');
const { patientList, patientUpdate } = require('./front/patient');
const { doctorList, doctorDetail, doctorUpdate, accountExit, accountUpgrade } = require('./front/member');
const { bannerList } = require('./front/banner');
const { appointmentList, appointmentInfo, appointmentTotal, appointmentAdd, appointmentUpdate, appointmentRemove } = require('./front/appointment');
const { commentList, commentTotal, commentAdd, commentRemove } = require('./front/comment');
const { businessList, businessInfo, businessUpdate } = require('./front/business');
const { postsList, postsTotal, postsInfo, postsSearch } = require('./front/posts');
const { noticeList, noticeTotal, noticeInfo, noticeRemove, noticeUpdate, noticeUpdateAll } = require('./front/notice')
const { followingList, followerList, followingAdd, followingRemove } = require('./front/follow')
const { replyList, replyTotal, replyAdd, replyRemove } = require('./front/reply')
const { alipay, alipayNotifyUrl } = require('./front/payment')
const { financeList, financeAdd, financeUpdate, financeRemove, financeAnalysisToday } = require('./front/finance')
const { orderList, orderTotal, orderUpdate, orderAdd, orderDetail } = require('./front/order')
const { inviteAdd, inviteList, inviteInfo, inviteUpdate, inviteRemove } = require('./front/invite')
// 创建路由实例
const router = express.Router();
// 短信验证码
router.get('/verifyCode', verifyCode);
// 登录
router.post('/login', login);
// 检测token是否合法
router.get('/checkToken', checkToken);
// token续费
router.post('/refreshToken', refreshToken);

// 获取医生列表
router.get('/doctorList', doctorList);
// 医生详情
router.get('/doctorDetail', doctorDetail);
// 评论列表
router.get('/commentList', commentList);
router.get('/commentTotal', commentTotal);

// 医疗机构主页
router.get('/businessList', businessList)
router.get('/businessInfo', businessInfo)

// 获取机构轮播图列表
router.get('/bannerList', bannerList);

// 支付宝异步通知
router.post('/alipayNotifyUrl', alipayNotifyUrl)


// 健康知识
router.get('/postsList', postsList)
router.get('/postsTotal', postsTotal)
router.get('/postsInfo', postsInfo)
router.get('/postsSearch', postsSearch)


// 文章回复管理
router.get('/replyList', replyList)
router.get('/replyTotal', replyTotal)
router.delete('/replyRemove', replyRemove)


// ***********登录鉴权自定义中间件************//
router.use((req, res, next) => {
    // 通过请求对象传递登录信息
    req.loginInfo = {};
    // 获取接口基础地址
    // const url = req.url.split('?').shift().replace('/front', '');
    // 如果访问的是需要登录授权的接口
    // 通过请求头获取登录凭证token
    const { authorization } = req.headers;
    // 未授权, 禁止访问
    if (!authorization) {
        return res.send({
            code: 403,
            msg: '未通过请求头Authorization携带登录凭证token',
            data: []
        });
    }
    try {
        var decoded = jwt.verify(authorization.replace('Bearer ', ''), secret);
        if (!decoded) {
            return res.send({
                code: 403,
                msg: '登录凭证token不合法',
                data: []
            });
        }
        const { id, phone, account_type, exp } = decoded;
        // 过期
        if (exp < parseInt(Date.now() / 1000)) {
            return res.send({
                code: 403,
                msg: '登录凭证token已过期请重新登录',
                data: []
            })
        }
        // 将登录信息通过请求对象, 继续向后传递
        req.loginInfo = { id, phone, account_type }
    } catch (err) {
        // 保证代码不保存
        req.loginInfo = {}
        // err
        return res.send({
            code: 403,
            msg: '登录凭证无效或已过期',
            data: []
        });
    }
    next();
})

// 企业加入邀请
router.get('/inviteList', inviteList)
router.get('/inviteInfo', inviteInfo)
router.put('/inviteAdd', inviteAdd)
router.post('/inviteUpdate', inviteUpdate)
router.delete('/inviteRemove', inviteRemove)

// 退出当前企业: 本质是更新账号信息
router.post('/accountExit', accountExit)
// 账号升级: 普通账号升级企业账号
router.post('/accountUpgrade', accountUpgrade)

// 发布评论
router.put('/replyAdd', replyAdd)

// 订单管理
router.get('/orderList', orderList)
router.get('/orderTotal', orderTotal)
router.get('/orderDetail', orderDetail)
router.put('/orderAdd', orderAdd)
router.post('/orderUpdate', orderUpdate)


// 获取资金流水
router.get('/financeList', financeList)
router.put('/financeAdd', financeAdd)
router.post('/financeUpdate', financeUpdate)
router.delete('/financeRemove', financeRemove)
router.delete('/financeAnalysisToday', financeAnalysisToday)

// 获取患者列表
router.get('/patientList', patientList);
// 更新医生信息
router.post('/patientUpdate', upload.single('avatar'), patientUpdate);
router.post('/doctorUpdate', upload.single('avatar'), doctorUpdate);

// 预约操作
router.get('/appointmentList', appointmentList);
router.get('/appointmentInfo', appointmentInfo);
router.get('/appointmentTotal', appointmentTotal);
router.put('/appointmentAdd', appointmentAdd);
router.post('/appointmentUpdate', appointmentUpdate);
router.delete('/appointmentRemove', appointmentRemove);

// 评论操作
router.put('/commentAdd', commentAdd);
router.delete('/commentRemove', commentRemove);

// 通知管理
router.get('/noticeList', noticeList)
router.get('/noticeTotal', noticeTotal)
router.get('/noticeInfo', noticeInfo)
router.post('/noticeUpdate', noticeUpdate)
router.post('/noticeUpdateAll', noticeUpdateAll)
router.delete('/noticeRemove', noticeRemove)

// 关注&&粉丝管理
router.get('/followingList', followingList)
router.get('/followerList', followerList)
router.put('/followingAdd', followingAdd)
router.delete('/followingRemove', followingRemove)

// 更新企业信息
router.post('/businessUpdate', upload.single('logo'), businessUpdate)
// 支付
router.post('/alipay', alipay)

module.exports = router;